﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmFlawList :FrmBase
    {
        private FlawService flawService = new FlawService();
        private List<Flaw> flaws = null;
        private string parentForm = "";
        private PlanDevice  planDevice;  //对应缺陷的巡视设备，为空则为所有缺陷
        private Device device = null;

        public FrmFlawList()
        {
            InitializeComponent();
            ttlbrhead.Text = ResourceImage.缺陷管理;
            ilstbcup.Images.Add(ResourceImage.s_增加01);
            ilstbcup.Images.Add(ResourceImage.s_修改01);
            ilstbcup.Images.Add(ResourceImage.s_验收01);
            ilstbcup.Images.Add(ResourceImage.s_返回01);

            ilstbcdown.Images.Add(ResourceImage.s_增加02);
            ilstbcdown.Images.Add(ResourceImage.s_修改02);
            ilstbcdown.Images.Add(ResourceImage.s_验收02);
            ilstbcdown.Images.Add(ResourceImage.s_返回02);

            btnPrior.UpImage = ResourceImage.上一页可点击;
            btnPrior.DownImage = ResourceImage.上一页点击;
            btnPrior.DisableImage = ResourceImage.上一页灰;
             btnNext.UpImage = ResourceImage.下一页可点击;
             btnNext.DownImage = ResourceImage.下一页点击;
             btnNext.DisableImage = ResourceImage.下一页灰;
        }

        private void FrmFlawList_Load(object sender, EventArgs e)
        {
            if (IsReturn)  
                LoadCacheData();
            else
                LoadData();

            tbDeviceName.Text = planDevice != null ? planDevice.DeviceName : "所有缺陷";

            if (planDevice != null)
            {
                flaws = flawService.GetFlawsByDevice(planDevice.DeviceID, AppLogic.LoginUser.UserName);
                //获取对应的设备
                DeviceService deviceService = new DeviceService();
                device = deviceService.GetDevice(planDevice.DeviceID);
            }
            else
                flaws = flawService.GetAllFlaws(AppLogic.LoginUser.UserName);
            dlFlaw.DataSource = flaws;
            dlFlaw.DataBind();

            SetButtonState();

        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        public void LoadCacheData()
        {
            parentForm = (string)Caches[0];
            planDevice  = (PlanDevice)Caches[1];
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            parentForm = (string)Parameter[0];
            planDevice = (PlanDevice)Parameter[1];
        }

        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:  //新增
                    AddFlaw();
                    break;
                case 1:  //修改
                    EditFlaw();
                    break;
                case 2:  //处理缺陷
                    DealFlaw();
                    break;
                case 3:
                    FormManage.GotoNext(parentForm, null, null, true);
                    break;
            }
        }

        /// <summary>
        /// 新增缺陷
        /// </summary>
        private void AddFlaw()
        {

            FormManage.GotoNext("FrmFlawEdit", new object[] {0, null, device }, new object[] { parentForm, planDevice }, false);
        }

        //修改缺陷
        private void EditFlaw()
        {
            if (dlFlaw.SelectItem == null)
                return;


            Flaw flaw = (Flaw)dlFlaw.SelectItem;

            if (flaw.DealState != "草稿")
            {
                mssdlgConfirm.ShowDialog(this, "只有新增的缺陷可以修改", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            if (device == null)
            {
                DeviceService deviceService = new DeviceService();
                device = deviceService.GetDevice(flaw.DeviceID);
            }


            FormManage.GotoNext("FrmFlawEdit", new object[] { 1, flaw, device }, new object[] { parentForm, planDevice }, false);
        }

        /// <summary>
        /// 处理缺陷
        /// </summary>
        private void DealFlaw()
        {
            if (dlFlaw.SelectItem == null)
                return;


            Flaw flaw = (Flaw)dlFlaw.SelectItem;

            if (flaw.DealState == "草稿")
            {
                mssdlgConfirm.ShowDialog(this, "新增的缺陷不能进行验收处理", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }



            FormManage.GotoNext("FrmFlawDeal", new object[] { flaw }, new object[] { parentForm, planDevice }, false);
        }

        private void dlFlaw_OnItemClick(HandRise.MBHOP.Controls.DataListItem item)
        {
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            dlFlaw.NextPage();
            SetButtonState();

        }

        private void btnPrior_Click(object sender, EventArgs e)
        {
            dlFlaw.PreviousPage();
            SetButtonState();

        }

        private void SetButtonState()
        {
            btnNext.Enabled = dlFlaw.PageCount > 0 && dlFlaw.PageIndex < dlFlaw.PageCount;
            btnPrior.Enabled = dlFlaw.PageCount > 0 && dlFlaw.PageIndex > 1;
            lblPageInfo.Text = dlFlaw.PageIndex.ToString() + "/" + dlFlaw.PageCount.ToString();
        }





    }
}